package o;

import java.util.Map;
import java.util.Map.Entry;

public class dhq {
    private static final byte[] ˊ = new byte[0];
    private static boolean ˋ = false;
    private static dhq ॱ;
    private int ˎ = -100;
    private final dhw ˏ;

    private dhq() {
        if (ˋ) {
            this.ˏ = new dht();
        } else {
            this.ˏ = new dhs();
        }
        ˏ();
        Logger.ˎ("BackgroundTaskTermManager", "mBkgTermVersion:" + this.ˎ);
    }

    public static void ॱ(boolean z) {
        ˋ = z;
    }

    public static dhq ॱ() {
        dhq o_dhq;
        synchronized (ˊ) {
            if (ॱ == null) {
                ॱ = new dhq();
            }
            o_dhq = ॱ;
        }
        return o_dhq;
    }

    private void ˏ() {
        if (!this.ˏ.ˊ()) {
            this.ˎ = dhu.ˏ().ॱ("background_term_version", -100);
        } else if (!dhu.ˏ().ॱॱ("background_term_version")) {
            Map ʿ = eua.ˏ().ʿ();
            if (ʿ != null) {
                for (Entry entry : ʿ.entrySet()) {
                    String str = (String) entry.getKey();
                    Object value = entry.getValue();
                    if (str.startsWith("agree_protocol") && (value instanceof Boolean) && ((Boolean) value).booleanValue()) {
                        Logger.ˎ("BackgroundTaskTermManager", "init history versin.");
                        dhu.ˏ().ˊ("background_term_version", this.ˏ.ˎ());
                        this.ˎ = this.ˏ.ˎ();
                        return;
                    }
                }
            }
            dhu.ˏ().ˊ("background_term_version", -100);
        } else if (diu.ˊ().ˋ()) {
            ʼ();
        } else {
            this.ˎ = dhu.ˏ().ॱ("background_term_version", -100);
        }
    }

    private void ʼ() {
        this.ˎ = dhu.ˏ().ॱ("background_term_version", -100);
        if (this.ˎ == -100) {
            Logger.ˎ("BackgroundTaskTermManager", "fix 900 bg term version");
            this.ˎ = this.ˏ.ˏ();
            dhu.ˏ().ˊ("background_term_version", this.ˎ);
        }
    }

    public boolean ˎ() {
        if (diu.ˊ().ˎ()) {
            return true;
        }
        if (!this.ˏ.ˊ() || this.ˎ < this.ˏ.ˏ()) {
            return false;
        }
        Logger.ˎ("BackgroundTaskTermManager", "Background Term agree to run background task.");
        return true;
    }

    public void ˋ() {
        if (ˋ) {
            int ˏ = this.ˏ.ˏ();
            if (this.ˎ != ˏ) {
                Logger.ˎ("BackgroundTaskTermManager", "agree bkg task:" + ˏ);
                dhu.ˏ().ˊ("background_term_version", ˏ);
                this.ˎ = this.ˏ.ˏ();
            }
        }
    }

    public void ˊ() {
        if (ˋ) {
            Logger.ˎ("BackgroundTaskTermManager", "reject bkg task");
            dhu.ˏ().ˊ("background_term_version", -100);
            this.ˎ = -100;
        }
    }
}
